$(function(e){
	dataInit();
});


function dataInit(){
	eventInit();
	$('#messageTable').bootstrapTable({
        url: path+'cd82cef7421742959e6694823b621b3e', method: 'post', striped: true, cache: false, pagination: true,
        contentType: "application/x-www-form-urlencoded", sidePagination: "server", pageSize: 10, uniqueId: "id", detailView: false,
        queryParams: function (params) {
        	return { limit: params.limit, offset: params.offset, searchParams : $("#searchParams").val()};
        },//传递参数（*）
        columns:[{field: 'Number', title: '序号', align: 'center', width: '50', formatter: function (value, row, index) {
			return index+1;
		}}, 
		{field: 'context', title: '回复内容', align: 'center', width: '150', formatter: function (value, row, index) {
        	return '<a style="word-wrap:break-word;">'+value+'</a>';
        }},
        {field: 'wetherClose', title: '是否正在运作', align: 'center', width: '150', formatter: function (value, row, index) {
        	if(value == 0){
        		value = "停止运作";
        	}else{
        		value = "正在运作";
        	}
        	return '<a style="word-wrap:break-word;">'+value+'</a>';
        }},
        {field: 'wechatKey', title: '关键字', align: 'center', width: '150', formatter: function (value, row, index) {
        	return '<a style="word-wrap:break-word;">'+value+'</a>';
        }},
        {field: 'wechatKeyType', title: '关键字类型', align: 'center', width: '150', formatter: function (value, row, index) {
        	return '<a style="word-wrap:break-word;">'+value+'</a>';
        }},
        {field: 'wetherComplet', title: '是否完全匹配', align: 'center', width: '150', formatter: function (value, row, index) {
        	if(value == 0){
        		value = "模糊匹配";
        	}else{
        		value = "完全匹配";
        	}
        	return '<a style="word-wrap:break-word;">'+value+'</a>';
        }},
        
        {field: 'name', title: '创建人', align: 'center', width: '150', formatter: function (value, row, index) {
        	return '<a style="word-wrap:break-word;">'+value+'</a>';
        }},
        {field: 'createTime', title: '创建时间', align: 'center', width: '150', formatter: function (value, row, index) {
        	return '<a style="word-wrap:break-word;">'+value+'</a>';
        }},
        
        {field: 'operate', title: '操作', align: 'center', width: '210', events: {
       	 'click .RoleOfA': function (e, value, row, index) {
       		     editWechatKeys(row.id);
               },
               'click .RoleOfB': function (e, value, row, index) {
               	qiao.bs.confirm("确定要删除此项吗？",function(){
               		deleteWechatKeys(row.id);
	                },function(){});
               },
       },
       formatter: function (value, row, index) {
       	return [
				'<button type="button" class="RoleOfA btn btn-default  btn-sm" style="margin-right:15px;">编辑</button>',
				'<button type="button" class="RoleOfB btn btn-default  btn-sm" style="margin-right:15px;">删除</button>',

   	        ].join('');
       }
       }],
        onLoadSuccess: function(data){  //加载成功时执行
        	if(data.returnCode!=0){
        		qiao.bs.msg({msg:data.returnMessage,type:'danger'});
        	}
        }
    });
	
	//添加关键字
	$("#addWechatKeysForm").bootstrapValidator({
		fields: {
			context: {validators: {notEmpty: {message: '回复内容不能为空'}}},
			wetherClose: {validators: {notEmpty: {message: '是否正在运作不能为空'}}},
			wechatKey: {validators: {notEmpty: {message: '关键字不能为空'}}},
			wechatKeyType: {validators: {notEmpty: {message: '关键字类型不能为空'}}},
			wetherComplet: {validators: {notEmpty: {message: '是否完全匹配不能为空'}}},
	    },
	    submitHandler: function (validator, form, submitButton) {
	    	var params = {
	    	    context:$("#context").val(),
	    	    wetherClose:$("#wetherClose").val(),
	    	    wechatKey:$("#wechatKey").val(),
	    	    wechatKeyType:$("#wechatKeyType").val(),
	    	    wetherComplet:$("#wetherComplet ").val(),
	    	};
	    	console.log(params);
	    	AjaxPostUtil.request({url:path+"edbd0f9c8ad84e019f745c1087068281",params:params,type:'json',callback:function(json){
				if(json.returnCode==0){
					qiao.bs.msg({msg:"关键字添加成功",type:'success'});
					$("#addWechatKeysMation").modal("hide");
					$('#messageTable').bootstrapTable('refresh',{url:path+'cd82cef7421742959e6694823b621b3e'});
				}else{
					qiao.bs.msg({msg:json.returnMessage,type:'danger'});
				}
			}});
			return false;
	    }
	});
	
	//修改关键字
	$('#updateWechatKeysForm').bootstrapValidator({
	    fields: {
	    	updatecontext: {validators: {notEmpty: {message: '回复内容不能为空'}}},
	    	updatewetherClose: {validators: {notEmpty: {message: '是否正在运作不能为空'}}},
	    	updatewechatKey: {validators: {notEmpty: {message: '关键字不能为空'}}},
	    	updatewechatKeyType: {validators: {notEmpty: {message: '关键字类型不能为空'}}},
	    	updatewetherComplet: {validators: {notEmpty: {message: '是否完全匹配不能为空'}}},
	    },
	    submitHandler: function (validator, form, submitButton) {
	    	var params = null;
				params = {
					rowId:rowIdRet,
					context:$("#updatecontext").val(),
					wetherClose:$("#updatewetherClose").val(),
					wechatKey:$("#updatewechatKey").val(),
					wechatKeyType:$("#updatewechatKeyType").val(),
					wetherComplet:$("#updatewetherComplet").val(),
				};
			console.log(params);
			AjaxPostUtil.request({url:path+"6125c37521ac49bf8da3e50d62b340e6",params:params,type:'json',callback:function(json){
				if(json.returnCode==0){
					qiao.bs.msg({msg:"保存成功",type:'success'});
					$("#updateWechatKeysMation").modal("hide");
					$('#messageTable').bootstrapTable('refresh',{url:path+'cd82cef7421742959e6694823b621b3e'});
				}else{
					qiao.bs.msg({msg:json.returnMessage,type:'danger'});
				}
			}});
			return false;
	    }
	});
}




function eventInit(){
	$('body').on('click', '#addWechatKeys', function(e){
		$("input").val("");
		
		$("#addWechatKeysMation").modal("show");
	});
	
	//回车模糊搜索
	$('body').keydown(function() {
        if (event.keyCode == "13") {//keyCode=13是回车键
        	var params = {
        			limit : 10,
        			offset : 0,
        			searchParams : $("#searchParams").val()
        	}
        	AjaxPostUtil.request({url : path+"cd82cef7421742959e6694823b621b3e", params : params, type : 'json', callback : function(json){
        		if(json.returnCode == 0){
        			console.log(json);
        			$('#messageTable').bootstrapTable('refresh',{url : path+'cd82cef7421742959e6694823b621b3e'});
        		}else{
        			qiao.bs.msg({msg : '参数错误.', type : 'danger'});
        		}
        	}});
        }
	});
}

//删除
function deleteWechatKeys(rowId){
	AjaxPostUtil.request({url : path+"9ce439221e864604821a47fb47f57146", params : {rowId : rowId}, type : 'json', callback : function(jsonMation){
		if(jsonMation.returnCode == 0){
			qiao.bs.msg({msg : "删除成功.", type : 'success'});
			$('#messageTable').bootstrapTable('refresh',{url : path+'cd82cef7421742959e6694823b621b3e'});
		}else{
			qiao.bs.msg({msg : "参数错误.", type : 'danger'});
		}
	}});
}

//编辑
function editWechatKeys(rowId){
	AjaxPostUtil.request({url:path+"8805a7e4259c4023a3eedc349eac6228",params:{rowId:rowId},type:'json',callback:function(json){
		console.log(json);
		if(json.returnCode==0){
			    rowIdRet = json.bean.id;
				$("#updatecontext").val(json.bean.context);
				$("#updatewetherClose").val(json.bean.wetherClose);
				if(json.bean.wetherClose == 0){
					$("#updatewetherClose").attr('停止运作');
				}else if(json.bean.wetherClose == 1){
					$("#updatewetherClose").attr('正在运作');
				}
				
				$("#updatewechatKey").val(json.bean.wechatKey);
				$("#updatewechatKeyType").val(json.bean.wechatKeyType);
				$("#updatewetherComplet").val(json.bean.wetherComplet);
				if(json.bean.wetherComplet == 0){
					$("#updatewetherComplet").attr('模糊匹配');
        		}else if(json.bean.wetherComplet == 1){
        			$("#updatewetherComplet").attr('完全匹配');
        		}
				
				$("#updateWechatKeysMation").modal("show");
		}else{
				qiao.bs.msg({msg:json.returnMessage,type:'danger'});
			}
	}});

		
}
